package com.aspire.moss.portal.desktop;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.desktop.AppInfo;
import com.aspire.moss.portal.desktop.IconCache;
import com.aspire.moss.portal.desktop.IconCache.IconCallback;
import com.aspire.moss.portal.desktop.Utils;

/**
 * 第一个单元格的内容,实现Observer观察workspace里的WorkspaceLock的变化,显示或者隐藏删除程序图标
 * 
 * @author Administrator
 * 
 */
public class AppView extends TextView implements Observer, OnClickListener, IconCallback {

    private static final String TAG = "AppView";

    private Context mContext;

    public AppInfo appInfo; // 保存当前的单元格信息

    private static Bitmap delBitmap; // 删除图标icon

//    private static Bitmap newVersion;// 有新版本icon
//
//    private static Bitmap downing;// 下载中的icon

    private static int iconWidth;

    private int clickX, clickY;

    private boolean con;

    /**
     * 图标
     * */
    public Bitmap iconBitmap;

    /**
     * 自己的点击事件
     */
    private OnClickListener onClickListener;

    /**
     * 删除图标的点事件
     */
    private OnDeleteClickListener onDeleteClickListener;

    private OnAppViewChangeListener onAppViewChangeListener;

    public AppView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppView);
        if (null == delBitmap) {
            delBitmap = ((BitmapDrawable) a.getDrawable(R.styleable.AppView_delBitmap)).getBitmap();
        }
//        if (null == newVersion) {
//            newVersion = ((BitmapDrawable) a.getDrawable(R.styleable.AppView_newVersion)).getBitmap();
//        }
//        if (null == downing) {
//            downing = ((BitmapDrawable) a.getDrawable(R.styleable.AppView_downing)).getBitmap();
//        }
//        if (iconWidth == 0) {
//            iconWidth = (int) a.getDimension(R.styleable.AppView_iconWidth, 3);
//        }
        super.setOnClickListener(this);
    }

    /**
     * 更新状态 the Object data type is AppInfo
     */
    @Override
    public void update(Observable observable, Object data) {
        Log.d(TAG, "update(Observable observable, Object data) data=" + data);
//        if (observable instanceof AppsObservable) {
//            final AppsObservable appsObservable = (AppsObservable) observable;
//
//        } else if (observable instanceof Workspace.WorkspaceLock && null != data && !appInfo.isDefaultApp()) {
            updateDelete((Boolean) data);
//        } else if (observable instanceof Workspace.WorkspaceLock && null != data && appInfo.isDefaultApp()) {
//            con = (Boolean) data;
//        }
    }

    /**/
    public void change(AppInfo appInfo) {
        this.change(appInfo, VISIBLE);
    }

    public void change(AppInfo appInfo, int isVisible) {
        setAppInfo(appInfo);
        updateDelete(con);
        this.setVisibility(isVisible);
        if(null != onAppViewChangeListener){
            onAppViewChangeListener.onAppViewChange(this);
        }
    }
    
    public void layoutChange(){
        if(null != onAppViewChangeListener){
            onAppViewChangeListener.onAppViewChange(this);
        }
    }

    /*
     * public void setIcon(Bitmap bitmap, boolean isDel) { Bitmap src = null; if
     * (isDel) { src = Bitmap.createBitmap(bitmap.getWidth() + top, bitmap
     * .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(src); final
     * int left = (bitmap.getWidth() - bitmap.getWidth()) / 2;
     * canvas.drawBitmap(bitmap, left, top, null); canvas.save();
     * canvas.drawBitmap(delBitmap, 0, 0, null); canvas.save(); } else { src =
     * Bitmap.createBitmap(bitmap.getWidth() + top, bitmap .getHeight(),
     * Config.ARGB_8888); Canvas canvas = new Canvas(src); final int left =
     * (bitmap.getWidth() - bitmap.getWidth()) / 2; canvas.drawBitmap(bitmap,
     * left, top, null); canvas.save(); } BitmapDrawable drawable = new
     * BitmapDrawable(src); BitmapDrawable press = new
     * BitmapDrawable(Utils.bitmapBrightness(src, -100)); StateListDrawable
     * stateListDrawable = new StateListDrawable();
     * stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET, press);
     * stateListDrawable.addState(View.ENABLED_FOCUSED_STATE_SET, press);
     * stateListDrawable.addState(View.ENABLED_STATE_SET, drawable);
     * stateListDrawable.addState(View.FOCUSED_STATE_SET, press);
     * stateListDrawable.addState(View.EMPTY_STATE_SET, drawable);
     * this.setCompoundDrawablesWithIntrinsicBounds(null, stateListDrawable,
     * null, null); }
     */
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent(MotionEvent event ) event=" + event);
        clickX = (int) event.getX();
        clickY = (int) event.getY();
        return super.onTouchEvent(event);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public OnDeleteClickListener getOnDeleteClickListener() {
        return onDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public OnAppViewChangeListener getOnAppViewChangeListener() {
        return onAppViewChangeListener;
    }

    public void setOnAppViewChangeListener(OnAppViewChangeListener onAppViewChangeListener) {
        this.onAppViewChangeListener = onAppViewChangeListener;
    }

    @Override
    public void onClick(View v) {
        if (inDelete(clickX, clickY) && null != onDeleteClickListener) {
            onDeleteClickListener.onDeleteClick(AppView.this);
        } else if (null != onClickListener && !con) {
            onClickListener.onClick(v);
        }
    }
    
    public StateListDrawable stateBitmap(Bitmap bitmap){
        final BitmapDrawable bitmapBrightness = new BitmapDrawable( Utils.bitmapBrightness(bitmap, -100));
        final BitmapDrawable drawable = new BitmapDrawable(bitmap);
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET, bitmapBrightness);
        stateListDrawable.addState(View.ENABLED_FOCUSED_STATE_SET, bitmapBrightness);
        stateListDrawable.addState(View.ENABLED_STATE_SET, drawable);
        stateListDrawable.addState(View.FOCUSED_STATE_SET, bitmapBrightness);
        stateListDrawable.addState(View.EMPTY_STATE_SET, drawable);
        return stateListDrawable;
    }

    public void setAppInfo(AppInfo appInfo) {
        this.appInfo = appInfo;
        this.setText(appInfo.app_name);
        iconBitmap = IconCache.getInsance().loadDrawable(mContext, appInfo.app_icon, this);
        if (null != iconBitmap) {
            updateIcon(iconBitmap);
        }
    }

    public void init() {

        this.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(this.iconBitmap), null, null);
    }

    /**
     * 判断用户是点击了删除还是本身
     * 
     * @param x
     * @param y
     * @return
     */
    private boolean inDelete(int x, int y) {
        final int delWidth = delBitmap.getWidth();
        final int delHeight = delBitmap.getHeight();
        if (x < delWidth && y < delHeight && con) {
            return true;
        }
        return false;
    }

    private Drawable updateIcon(Bitmap bitmap) {
        final int width = iconBitmap.getWidth();
        final int height = iconBitmap.getHeight();
        final Bitmap topBitmap = Bitmap.createBitmap(width + iconWidth, height + iconWidth / 2, Bitmap.Config.ARGB_4444);
        final Canvas canvas = new Canvas(topBitmap);
        canvas.drawBitmap(iconBitmap, iconWidth / 2, iconWidth / 2, null);
        canvas.save();
        this.iconBitmap = topBitmap;
        Drawable topDrawable = stateBitmap(topBitmap);
        this.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
        return topDrawable;
    }

    private Drawable updateDelete(boolean isDelete) {
        con = isDelete;
        Drawable topDrawable = null;
        if (null != iconBitmap && isDelete) {
            Bitmap bitmap = iconBitmap.copy(Config.ARGB_4444, true);
            final Canvas canvas = new Canvas(bitmap);
//            canvas.drawBitmap(iconBitmap, 0, 0, null);
            canvas.drawBitmap(delBitmap, 0, 0, null);
            canvas.save();
//            canvas.
            topDrawable = stateBitmap(bitmap);
            this.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
            if(null != bitmap && bitmap.isRecycled()){
                bitmap.recycle();
                bitmap = null;
            }
        } else {
            topDrawable = stateBitmap(iconBitmap);
            this.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
        }
        return topDrawable;
    }

    // private Drawable updateState(AppInfo appInfo){
    // BitmapDrawable srcTopDrawable = (BitmapDrawable) updateIcon(appInfo);
    // if(null != srcTopDrawable){
    // final Bitmap srcTopBitmap = srcTopDrawable.getBitmap();
    // final Canvas canvas = new Canvas(srcTopBitmap);
    // canvas.drawBitmap(delBitmap, srcTopBitmap.getWidth() - iconWidth, 0,
    // null);
    // canvas.save();
    // srcTopDrawable = new BitmapDrawable(srcTopBitmap);
    // }
    // return srcTopDrawable;
    // }

    /**
     * 点击删除图标的事件接口
     * 
     * @author Administrator
     * 
     */
    public interface OnDeleteClickListener {

        void onDeleteClick(AppView view);

    }

    /**
     * 当图标位置被改变时事件接口
     * 
     * @author Administrator
     * 
     */
    public interface OnAppViewChangeListener {

        void onAppViewChange(AppView view);

    }

    @Override
    public void iconLoadFinish(Bitmap iconDrawable) {
        this.iconBitmap = iconDrawable;
        updateIcon(iconBitmap);
    }

    public void setSuperOnClickListener(OnClickListener onClickListener) {
        super.setOnClickListener(onClickListener);
    }
}

enum AppViewAction {
    MOVE, SWAP, RESET
}
